﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Xml;

namespace GeoSketch2.Core
{
    /// <summary>
    /// The base polyline object
    /// </summary>
    public class Polyline : BaseDrawableObject
    {
        #region XMLElementsName
#pragma warning disable 1591
        public const string XML_Vertices = "Vertices";
#pragma warning restore 1591
        #endregion

        #region Fields
        /// <summary>
        /// The vertices list of the polyline
        /// </summary>
        public List<Vertex> Vertices; 
        #endregion

        #region Constructor

        /// <summary>
        /// The polyline object for the Archaeo module
        /// </summary>
        /// <param name="Vertices">The vertices list of the polyline, if null a new polyline will be created</param>
        /// <param name="Layer">The layer of the polyline</param>
        public Polyline(List<Vertex> Vertices, Layer Layer)
            : this(Vertices, Layer, LineTypeByLayer())
        {
        } 

        /// <summary>
        /// The polyline object for the Archaeo module
        /// </summary>
        /// <param name="Vertices">The vertices list of the polyline, if null a new polyline will be created</param>
        /// <param name="Layer">The layer of the polyline</param>
        /// <param name="LineType">The type of line of the polyline</param>
        public Polyline(List<Vertex> Vertices, Layer Layer, LineType LineType)
            : this(Vertices, Layer, LineType, ColorByLayer())
        {
        } 

        /// <summary>
        /// The polyline object for the Archaeo module
        /// </summary>
        /// <param name="Vertices">The vertices list of the polyline, if null a new polyline will be created</param>
        /// <param name="Layer">The layer of the polyline</param>
        /// <param name="LineType">The type of line of the polyline</param>
        /// <param name="Color">The color of the polyline</param>
        public Polyline(List<Vertex> Vertices, Layer Layer, LineType LineType, Color Color)
            : base(Layer, Color, LineType)
        {
            if (Vertices == null)
            {
                Vertices = new List<Vertex>();
            }
            this.Vertices = Vertices;
        }
        #endregion

        #region Methods
        /// <summary>
        /// The drawing method of the polyline when selected
        /// </summary>
        /// <param name="Viewport">The viewport to draw to</param>
        protected override void DrawSelected(BaseViewport Viewport)
        {
            DrawCustomPen(Viewport, Core.Vars.Setup.Tools.TSelection.SelectionPen);
        }

        /// <summary>
        /// The drawing method of the polyline when not selected
        /// </summary>
        /// <param name="Viewport">The viewport to draw to</param>
        protected override void DrawUnselected(BaseViewport Viewport)
        {
            DrawCustomPen(Viewport, Pen);
        }

        /// <summary>
        /// The draw method for each object with a custom pen
        /// </summary>
        /// <param name="Viewport">The viewport to draw to</param>
        /// <param name="CustomPen">The custom pen object to draw the object with</param>
        public override void DrawCustomPen(BaseViewport Viewport, Pen CustomPen)
        {
            Arc temp_arc;
            for (int i = 0; i < Vertices.Count - 1; i++)
            {
                if (Vertices[i].Type == VertexType.Line)
                {
                    Viewport.Graphic.DrawLine(CustomPen,
                       Viewport.Object2ImageTransformation.Transform(Vertices[i].Position),
                       Viewport.Object2ImageTransformation.Transform(Vertices[i + 1].Position));
                }
                else if (Vertices[i].Type == VertexType.Arc)
                {
                    if (Vertices.Count > i + 2)
                    {
                        temp_arc = new Arc(Viewport.Object2ImageTransformation.Transform(Vertices[i].Position),
                                Viewport.Object2ImageTransformation.Transform(Vertices[i + 1].Position),
                                Viewport.Object2ImageTransformation.Transform(Vertices[i + 2].Position));
                        Viewport.Graphic.DrawArc(CustomPen, temp_arc.EllipseBound, temp_arc.StartAngle, temp_arc.SweepAngle);
                        i++;
                    }
                }
            }
        }

        /// <summary>
        /// The method to return whether the object is near to the clic or not
        /// </summary>
        /// <param name="Clic">The point clicked in object coordinates</param>
        /// <param name="IsNearDistance">The distance under which the clic is near to the object</param>
        public override bool IsNear(Point2D64D Clic, double IsNearDistance)
        {
            for (int i = 0; i < Vertices.Count - 1; i++)
            {
                if (Vertices[i].Type == VertexType.Line)
                {
                    if (Clic.DistancePointLine(Vertices[i].Position, Vertices[i + 1].Position) < IsNearDistance)
                    {
                        Point2D64D center = (Vertices[i].Position + Vertices[i + 1].Position) * 0.5;
                        if (2 * center.Distance2DSqr(Clic) < Vertices[i].Position.Distance2DSqr(Vertices[i + 1].Position) + Core.Vars.Setup.Tools.TSelection.IsNearDistance * Core.Vars.Setup.Tools.TSelection.IsNearDistance)
                        {
                            return true;
                        }
                    }
                }
                else if (Vertices[i].Type == VertexType.Arc)
                {
                    i++;
                }
            }
            return false;
        }

        /// <summary>
        /// Returns the nearest line to the click, null if none
        /// </summary>
        /// <param name="Clic">The point clicked in object coordinates</param>
        /// <param name="IsNearDistance">The distance under which the clic is near to the object</param>
        public override Line2D64D NearestLine(Point2D64D Clic, double IsNearDistance)
        {
            Line2D64D dst=null;

            for (int i = 0; i < Vertices.Count - 1; i++)
            {
                if (Vertices[i].Type == VertexType.Line)
                {
                    if (Clic.DistancePointLine(Vertices[i].Position, Vertices[i + 1].Position) < IsNearDistance)
                    {
                        Point2D64D center = (Vertices[i].Position + Vertices[i + 1].Position) * 0.5;
                        if (2 * center.Distance2DSqr(Clic) < Vertices[i].Position.Distance2DSqr(Vertices[i + 1].Position) + Core.Vars.Setup.Tools.TSelection.IsNearDistance * Core.Vars.Setup.Tools.TSelection.IsNearDistance)
                        {
                            dst = new Line2D64D(Vertices[i].Position, Vertices[i + 1].Position);
                            break;
                        }
                    }
                }
                else if (Vertices[i].Type == VertexType.Arc)
                {
                    i++;
                }
            }
            return dst;
        }

        /// <summary>
        /// Returns the nearest point to the click, null if none
        /// </summary>
        /// <param name="Clic">The point clicked in object coordinates</param>
        /// <param name="IsNearDistance">The distance under which the clic is near to the object</param>
        public override Point2D64D NearestPoint(Point2D64D Clic, double IsNearDistance)
        {
            Point2D64D dst=null;

            for (int i = 0; i < Vertices.Count; i++)
            {
                if (Clic.Distance2D(Vertices[i].Position) < IsNearDistance)
                {
                    dst = Vertices[i].Position;
                }
            }

            return dst;
        }

        /// <summary>
        /// Clones the object
        /// </summary>
        public override T Clone<T>()
        {
            return (T)(object)new Polyline(Vertices.DeepClone<List<Vertex>>(), Layer, this._LineType, this._ObjectColor);
        }

        /// <summary>
        /// Translates the object
        /// </summary>
        public override void Translate(Point2D64D Vector)
        {
            foreach (Vertex v in Vertices)
            {
                v.Position.X += Vector.X;
                v.Position.Y += Vector.Y;
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Get the vertices list as an array of Point2Ds
        /// </summary>
        public Point2D64D[] VerticesPoints
        {
            get
            {
                Point2D64D[] dst = new Point2D64D[Vertices.Count];

                for (int i = 0; i < Vertices.Count; i++)
                {
                    dst[i] = Vertices[i].Position;
                }

                return dst;
            }
        }

        /// <summary>
        /// Gets the bounding box of the object, the Y-axis direction of the REctangleD must be upside
        /// </summary>
        public override RectangleD Bounds
        {
            get
            {
                return Tools.GetRectangleDBounds(VerticesPoints, YAxisDirection.UpSide);
            }
        }
        #endregion
    }
}
